package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.signature.common.ro.manager.ChartRo;
import com.credithc.signature.common.vo.manager.ChartVo;
import com.credithc.signature.manager.service.AuthorizationService;
import com.credithc.signature.manager.service.ChartService;
import com.credithc.signature.manager.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * 描 述: 统计查询Controller<br/>
 * 作 者: JingBo Hu<br/>
 * 创 建：2019/5/24 16:55<br/>
 * 版 本：v1.0.0<br>
 */
@RestController
@RequestMapping("/manager/scs/v2")
@Slf4j
public class StatisticsController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    ChartService iconService;

    @Autowired
    StatisticsService statisticsService;

    @Autowired
    AuthorizationService authorizationService;


    /**
     * 统计总量
     *
     * @param token
     * @param request
     * @param chartRo
     * @return
     * @throws ParseException
     */
    @GetMapping("/tenant/day")
    public Message<ChartVo> dayStatistics(@RequestHeader("token") String token, HttpServletRequest request, ChartRo chartRo) throws ParseException {
        List<String> channelNos = null;
        try {
            channelNos = authorizationService.authChannelNo(token, request);
        } catch (Exception e) {
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, e.getMessage());
        }

        if (channelNos == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权渠道为空！");
        } else {
            ChartVo chartVo = statisticsService.dayStatistics(chartRo.getStart(), chartRo.getStop(), channelNos);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, chartVo);
        }
    }

    /**
     * PV调用量
     *
     * @param token
     * @param request
     * @return
     * @throws ParseException
     */
    @GetMapping("/tenant/pv/hour")
    public Message<ChartVo> pvStatistics(@RequestHeader("token") String token, HttpServletRequest request) throws ParseException {
        List<String> channelNos = null;
        try {
            channelNos = authorizationService.authChannelNo(token, request);
        } catch (Exception e) {
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, e.getMessage());
        }

        if (channelNos == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权渠道为空！");
        } else {
            ChartVo chartVo = statisticsService.pvStatistics(channelNos);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, chartVo);
        }
    }

    /**
     * 业务系统调用量
     *
     * @param token
     * @param request
     * @return
     * @throws ParseException
     */
    @GetMapping("/tenant/channel/hour")
    public Message<ChartVo> systemStatistics(@RequestHeader("token") String token, HttpServletRequest request) throws ParseException {
        List<String> channelNos = null;
        try {
            channelNos = authorizationService.authChannelNo(token, request);
        } catch (Exception e) {
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, e.getMessage());
        }
        if (channelNos == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权渠道为空！");
        } else {
            ChartVo chartVo = statisticsService.systemStatistics(channelNos);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, chartVo);
        }
    }

}
